
package coms.dataaccess.hibernate;

import coms.dataaccess.dao.ConferenceItemDAO;
import coms.dto.*;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

public class ConferenceItemDAOImpl extends GenericDAOImpl<ConferenceItem, Integer> implements ConferenceItemDAO {

    private Session session;
    
    @Override
    public List<ConferenceItem> search(ConferenceItem conferenceItem) throws SQLException {
           session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(ConferenceItem.class,"confItem");
       
        if (conferenceItem == null) criteria.list();
        
        if (conferenceItem.getConference()!=null){
            criteria.createAlias("confItem.conference", "conf")
                     .add(Restrictions.eq("conf.id", conferenceItem.getConference().getId()));
        }
        if (conferenceItem.getTopic()!=null && !conferenceItem.getTopic().equals("")){
            criteria.add(Restrictions.like("confItem.topic", "%"+conferenceItem.getTopic()+"%"));
        }
        
         if (conferenceItem.getConferenceItemType()!=null){
            criteria.createAlias("confItem.conferenceItemType", "type")
                     .add(Restrictions.eq("type.id", conferenceItem.getConferenceItemType().getId()));
        }
        
               
        return criteria.list();
    }
    
    @Override
    public List<ConferenceItem> searchBookedConferenceItemByParticipantAndConference(User user, Conference conference) {
          session = HibernateUtil.getSessionFactory().openSession();
        Criteria criteria = session.createCriteria(ConferenceItemBooking.class,"confItemBooking");
        if (user!=null){
             criteria.createAlias("confItemBooking.participant", "p")
                     .add(Restrictions.eq("p.id", user.getId()));
        }
        
        if (conference!=null){
            criteria.createAlias("confItemBooking.conferenceItem", "confItem")
                    .createAlias("confItem.conference", "conf")
                     .add(Restrictions.eq("conf.id", conference.getId()));
        }
        
          criteria.setProjection(Projections.property("confItemBooking.conferenceItem"));
               
        return criteria.list();
    }
    
}
